import 'package:dio/dio.dart';
import 'package:logger/logger.dart';

import '../../../data/dio/api/bt_dio_network.dart';
import '../../../data/dio/api/bt_net_data_source.dart';
import '../../../data/models/bt_goods.dart';
import '../../../data/models/bt_recharge.dart';
import '../../../data/stats/bt_log_pages.dart';
import '../../../data/stats/bt_log_pv_manger.dart';

class BtOrderRepository {
  final logger = Logger();

  final BtNetDataSource network = BtDioNetwork();

  void fetchBtGoodsList(int page, int limit, Function(List<BtGoods> btBtGoodsList) onResult) {
    var map = <String, dynamic>{};
    map['isIncludeSubscription'] = false;
    map['payChannel'] = 'IAP';
    network.fetchCoinGoodsList(map).then((btBtGoodsResult) {
      if (btBtGoodsResult.isSuccess) {
        onResult(btBtGoodsResult.data ?? []);

        BtLogPvManger.stat(page: BtLogPages.purchase);
      }
    }).catchError((error) {
      // non-200 error goes here.
      switch (error.runtimeType) {
        case DioException:
          // Here's the sample to get the failed response error code and message
          final res = (error as DioException).response;
          logger.e('Got error : ${res?.statusCode} -> ${res?.statusMessage}');
          break;
        default:
          logger.e('Error occurred: $error');
          break;
      }
    });
  }

  void recharge(String goodsCode, Function(bool success, String msg, BtRecharge? data) onResult) {
    var map = <String, String>{};
    map['entry'] = '';
    map['source'] = '';
    map['payChannel'] = 'IAP';
    map['goodsCode'] = goodsCode;

    network.recharge(map).then((result) {
      if (result.isSuccess) {
        BtLogPvManger.stat(page: BtLogPages.createOrder);
      }
      onResult(result.isSuccess, result.msg, result.data);
    }).catchError((error) {
      // non-200 error goes here.
      switch (error.runtimeType) {
        case DioException:
          // Here's the sample to get the failed response error code and message
          final res = (error as DioException).response;
          logger.e('Got error : ${res?.statusCode} -> ${res?.statusMessage}');
          break;
        default:
          logger.e('Error occurred: $error');
          break;
      }
    });
  }

  void rechargePayment(
      String orderNo, String payload, String transactionId, Function(bool success, String msg) onResult) {
    var map = <String, String>{};
    map['orderNo'] = orderNo;
    map['payload'] = payload;
    map['transactionId'] = transactionId;
    map['type'] = '1';

    network.rechargePayment(map).then((result) {
      if (result.isSuccess) {}
      onResult(result.isSuccess, result.msg);
    }).catchError((error) {
      // non-200 error goes here.
      switch (error.runtimeType) {
        case DioException:
          // Here's the sample to get the failed response error code and message
          final res = (error as DioException).response;
          logger.e('Got error : ${res?.statusCode} -> ${res?.statusMessage}');
          break;
        default:
          logger.e('Error occurred: $error');
          break;
      }
    });
  }
}
